<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\GridGroup;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;

// 网格组
class GridGroupController extends Controller
{
    // 列表
    public function index(Request $request)
    {
        $is_all = $request->input('is_all', 0); //不分页获取全部数据

        $keywords = $request->input('keywords', ''); // 查询关键字
        $village_id = $request->input('village_id', '');

        if($is_all){
            $data = GridGroup::query()
            ->when($keywords, function ($query) use ($keywords) {
                $query->where('name', 'like', "%{$keywords}%");
            })
            ->when($village_id, function ($query) use ($village_id) {
                $query->whereIn('village_id', explode(',',$village_id));
            })
            ->where($this->getAppWhere())
             ->orderBy('id')
            ->get()
            ->toArray();
        }else{
            $data = GridGroup::query()
                ->when($keywords, function ($query) use ($keywords) {
                    $query->where('name', 'like', "%{$keywords}%");
                })
                ->when($village_id, function ($query) use ($village_id) {
                    $query->whereIn('village_id', explode(',',$village_id));
                })
                ->where($this->getAppWhere())
                ->orderByDesc('id')
                ->paginate(env('PAGE_SIZE', 10));
        }    
        return showData($data);    
    }

    // 新增
    public function store(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'name' => 'required|max:20',
            'street_id' => 'required',
            'village_id' => 'required',
            'project_id' => 'required',
            'segment_id' => 'required',
        ], [
            'name.required' => '请输入名称',
            'street_id.required' => '请输入街道',
            'village_id.required' => '请输入村庄',
            'project_id.required' => '请输入项目',
            'segment_id.required' => '请输入号段',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $data = $validate->getData();

        $Info['name']=$data['name'];
        $Info['street_id']=$data['street_id'];
        $Info['village_id']=$data['village_id'];
        $Info['project_id']=$data['project_id'];
        $Info['segment_id']=$data['segment_id'];

        try {
            $add=GridGroup::query()->create($Info);
            return showSuccess('添加成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 编辑
    public function update($Id, Request $request)
    {
        if (empty($Id)) {
            return showError('id不能为空!');
        }
        // 验证
        $validate = Validator::make($request->all(), [
            'name' => 'required|max:20',
            'street_id' => 'required',
            'village_id' => 'required',
            'project_id' => 'required',
            'segment_id' => 'required',
        ], [
            'name.required' => '请输入名称',
            'street_id.required' => '请输入街道',
            'village_id.required' => '请输入村庄',
            'project_id.required' => '请输入项目',
            'segment_id.required' => '请输入号段',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $data = $validate->getData();

        $Info['name']=$data['name'];
        $Info['street_id']=$data['street_id'];
        $Info['village_id']=$data['village_id'];
        $Info['project_id']=$data['project_id'];
        $Info['segment_id']=$data['segment_id'];
        try {
            $update = GridGroup::query()->where('id', $Id)->update($Info);
            return showSuccess('编辑成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 删除
    public function destroy($id)
    {
        if (empty($id)) {
            return showError('参数错误！');
        }

        $admin = GridGroup::query()->where('id', $id)->first();
        if (empty($admin)) {
            return showError('删除项不存在');
        }
        try {
            $delete = GridGroup::query()->where('id', $id)->delete();
            return showSuccess('删除成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 详情
    public function show($id)
    {
        if (empty($id)) {
            return showError('参数错误');
        }
   
        $data = GridGroup::query()
            ->where('id', $id)
            ->first();

        if (empty($data)) {
            return showError('获取信息失败');
        }

        return showData($data);
    }
}

